using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace ExcelRefresh.Com
{
    public static class AdoExtensions
    {
        [DebuggerStepThrough]
        public static IEnumerable<T> RunQuery<T>(this ADODB.Connection connection, string query, Func<ADODB.Fields, T> materialize)
        {
            object recordsAffected;
            var rs = connection.Execute(query, out recordsAffected);
            try
            {
                while (!rs.EOF)
                {
                    yield return materialize(rs.Fields);
                    rs.MoveNext();
                }
            }
            finally
            {
                rs.Close();
            }
        }
    }
}